
import { _decorator, Component, Node, Vec2, Vec3 } from 'cc';
import { MonsterManager } from '../monster/MonsterManager';
import { EventName } from '../../manager/AppControll';
import EvtManger from '../../tools/EvtManger';
import { Move } from '../component/Move';
import { MonsterOrigin } from '../monster/MonsterOrigin';
import { Tools } from '../../tools/Tools';
const { ccclass, property } = _decorator;

//炮台基类
@ccclass('GunOrigin')
export class GunOrigin extends Component {
    protected mCurTime = 0;
    protected mSkillTime = 0;
    protected mNdGun: Node = null;
    protected mTarget: Node = null;
    protected barrelLength: number = 0;

    protected onLoad(): void {

    }

    //炮台事件
    RegisteredEvent() {
        EvtManger.on(EventName.monsterDeath, this, this.MonsterDrath);
    }

    //攻击对象死亡出发事件
    MonsterDrath(node: Node) {
        if (!this.mTarget) return;
        if (this.mTarget === node) {
            this.mTarget = null;
        }
    }

    //查找攻击对象
    FindTarget() {
        //如果已有攻击对象则直接返回
        if (this.mTarget) return

        let nearestNode = null;
        let minDistance = Infinity; //无限大的一个数

        let lt = Tools.GetLtMonster();
        for (let i = 0; i < lt.length; i++) {
            let distance = this.node.position.clone().subtract(lt[i].position).length();
            if (distance < minDistance && !lt[i].getComponent(MonsterOrigin).IsDeath) {
                minDistance = distance;
                nearestNode = lt[i];
            }
        }
        if (nearestNode && nearestNode.active) {
            this.mTarget = nearestNode;
        }

        if (this.mTarget) {
            this.mTarget.getComponent(MonsterOrigin).BeLock();
        }
        EvtManger.do(EventName.setTarget, this.mTarget);
    }

    Shoot(point: Vec2, t: number) {
    }

    GetHitPositinByTime(t: number) {
        if (t) {
            let point = this.mTarget.getComponent(Move).GetPointByTime(t);
            return point;
        } else {
            return null;
        }
    }
}
